﻿#region Legal notice
/*
 * This file is part of EPPLib.
 * Copyright 2010, 2011 
 * Gianluca Esposito (info@gianlucaesposito.it)
 * ----------------------------------------------------------------------------
 * EPPLib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * EPPLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Nome-Programma; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#endregion

#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Linq;
#endregion

namespace EPPLib.Messages
{
    public abstract class BaseContainer
    {
        #region Constants
        protected const string XML_VERSION = "1.0";
        protected const string XML_ENCODING = "UTF-8";
        protected const string XML_STANDALONE = "no";
        protected const string ROOT_ELEMENT_NAME = "epp";
        protected const string ROOT_ELEMENT_NAME_NAMESPACE = "urn:ietf:params:xml:ns:epp-1.0";
        #endregion

        #region Members
        protected bool _useSchemaNamespace = true;
        #endregion

        #region Properties
        public bool UseSchemaNamespace { get { return _useSchemaNamespace; } set { _useSchemaNamespace = value; } }
        #endregion

        #region Public methods
        public XDocument Serialize()
        {
            XNamespace rootNamespace = ROOT_ELEMENT_NAME_NAMESPACE;
            XNamespace schemaNamespace = "http://www.w3.org/2001/XMLSchema-instance";
            XDocument document = null;
            if (_useSchemaNamespace)
            {
                document = new XDocument(
                new XDeclaration(XML_VERSION, XML_ENCODING, XML_STANDALONE),
                    new XElement(rootNamespace + ROOT_ELEMENT_NAME,
                        new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                        new XAttribute(schemaNamespace + "schemaLocation", "urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd"))
            );
            }
            else
            {
                document = new XDocument(
               new XDeclaration(XML_VERSION, XML_ENCODING, XML_STANDALONE),
                   new XElement(rootNamespace + ROOT_ELEMENT_NAME));
            }

            return document;
        }
        #endregion
    }
}